//
//  HighVioViewController.m
//  CarLife
//
//  Created by yu on 2018/7/5.
//  Copyright © 2018年 jpas. All rights reserved.
//

#import "HighVioViewController.h"
#import "HighVioModel.h"
#import "HighvioView.h"
#import "HighvioAnnotationView.h"

@interface HighVioViewController ()

@property (nonatomic, strong) MAPointAnnotation *centerAnnotation;

@property (nonatomic, assign) BOOL isUserLocation;

@property (nonatomic, copy) NSString *latitude;
@property (nonatomic, copy) NSString *longitude;

@property (nonatomic, strong) NSMutableArray *dataSource;

@property (nonatomic, strong) KLCPopup *popup;
@property (nonatomic, strong) HighvioView *vioView;

@end

@implementation HighVioViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"违章高发地";
    [self setLeftImageNamed:@"back_gray" action:@selector(returnAction)];
    [self initMapView];
    [self setupButton];
    [self.mapView setShowsUserLocation:YES];
    [self.mapView setZoomLevel:13];
    
    [self setupCenterAnnotation];
    
    _isUserLocation = YES;
    
    [self loadHighVioData];
    
    [self setupPopupView];
}

- (void)setupCenterAnnotation
{
    UIImageView *imgView = [[UIImageView alloc] initWithImage:IMAGE_NAMED(@"highvio_centerpoint")];
    imgView.center = CGPointMake(self.view.centerX, self.view.centerY-50);
    [self.view addSubview:imgView];
}

- (void)setupPopupView
{
    self.vioView = [[HighvioView alloc] initWithFrame:CGRectMake(0, kScreenHeight-64, kScreenWidth, 100)];
    [self.view addSubview:self.vioView];
    
    @weakify(self);
    [self.vioView setCancelBlock:^{
        @strongify(self);
        //取消选中
        for (id<MAAnnotation> annotation in self.mapView.annotations) {
            [self.mapView deselectAnnotation:annotation animated:YES];
        }
        [self showHighvioView:nil isSelect:NO];
    }];
}

- (void)loadHighVioData {
    
    if (_isUserLocation) {
        _latitude = [User sharedUser].latitude;
        _longitude = [User sharedUser].longitude;
    } else {
        _latitude = NSStringFormat(@"%lf",self.mapView.centerCoordinate.latitude);
        _longitude = NSStringFormat(@"%lf",self.mapView.centerCoordinate.longitude);
    }
    
    [self.requestTool loadHighVioByLat:_latitude lng:_longitude num:@"15" range:@"2500" success:^(NSMutableArray *vios) {
        [self addAnnotationsToMap:vios];
    } failure:^(NSString *error) {
        [MBProgressHUD showToast:@"附近无违章高发地"];
    }];
}

- (void)addAnnotationsToMap:(NSMutableArray *)vios
{
    NSMutableArray *beforeAnnotations = [NSMutableArray arrayWithArray:self.mapView.annotations];
    [beforeAnnotations removeObject:[self.mapView userLocation]];
    [self.mapView removeAnnotations:beforeAnnotations];
    
    NSMutableArray *coordinates = [NSMutableArray array];
    for (HighVioModel *vioM in vios) {
        vioM.coordinate = CLLocationCoordinate2DMake([vioM.lat doubleValue], [vioM.lng doubleValue]);
        vioM.title = @"highvio";
        [coordinates addObject:vioM];
    }
    self.dataSource = coordinates;
    [self.mapView addAnnotations:coordinates];
}

- (void)mapView:(MAMapView *)mapView mapDidMoveByUser:(BOOL)wasUserAction
{
    if (!wasUserAction) {
        return;
    }
    PPLog(@"mapview center = %lf-%lf",self.mapView.centerCoordinate.latitude,self.mapView.centerCoordinate.longitude);

    _isUserLocation = NO;
    [self loadHighVioData];
}

- (MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id<MAAnnotation>)annotation
{
    if ([annotation isKindOfClass:[MAUserLocation class]]) {
        return nil;
    }
    
    if ([annotation isKindOfClass:[HighVioModel class]])
    {
        static NSString *pointReuseIndetifier = @"pointReuseIndetifier";
        HighvioAnnotationView *annotationView = (HighvioAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:pointReuseIndetifier];
        if (annotationView == nil)
        {
            annotationView = [[HighvioAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:pointReuseIndetifier];
            
            annotationView.canShowCallout = NO;
        }
        
        annotationView.dataSource = self.dataSource;

        // 大头针点击回调
        @weakify(self);
        [annotationView setSelectHighvioViewBlock:^(HighVioModel *vioM, BOOL isSelect){
            @strongify(self);
            [self showHighvioView:vioM isSelect:isSelect];
        }];
        
        return annotationView;
    }
    return nil;
}


#pragma - Select Annotation Action
- (void)showHighvioView:(HighVioModel *)vioM isSelect:(BOOL)isSelect{
    if (vioM) {
        self.vioView.reasonLabel.text = NSStringFormat(@"%@(累计%@次)",vioM.content,vioM.num);
        self.vioView.addressLabel.text = NSStringFormat(@"%@%@",vioM.town,vioM.address);
    }
    
    if (self.vioView.sd_y != kScreenHeight-164 && isSelect) {
        [UIView animateWithDuration:0.2 animations:^{
            self.vioView.sd_y = kScreenHeight - 164;
            self.zoomBtnContainer.sd_layout
            .bottomSpaceToView(self.view, 115);
            [self.zoomBtnContainer updateLayout];
        }];
    }
    
    if (!isSelect) {
        [UIView animateWithDuration:0.2 animations:^{
            self.vioView.sd_y = kScreenHeight + 164;
            self.zoomBtnContainer.sd_layout
            .bottomSpaceToView(self.view, 15);
            [self.zoomBtnContainer updateLayout];
        }];
    }
    
}

- (void)mapView:(MAMapView *)mapView didDeselectAnnotationView:(MAAnnotationView *)view
{
    [mapView deselectAnnotation:view.annotation animated:YES];
}

- (void)mapView:(MAMapView *)mapView didSelectAnnotationView:(MAAnnotationView *)view
{
    if ([view.annotation isKindOfClass:[MAUserLocation class]]) {
        return;
    }
    
    HighVioModel *vioM = (HighVioModel *)view.annotation;
    
    PPLog(@"%@(累计%@次)",vioM.content,vioM.num);
    
//    self.vioView.reasonLabel.text = NSStringFormat(@"%@(累计%@次)",vioM.content,vioM.num);
//    self.vioView.addressLabel.text = NSStringFormat(@"%@%@%@",vioM.city,vioM.town,vioM.address);
//
//    if (self.vioView.sd_y == kScreenHeight-100-64) {
//        [UIView animateWithDuration:0.2 animations:^{
//
//            CGRect frame = self.zoomBtnContainer.frame;
//            self.zoomBtnContainer.sd_y = frame.origin.y + 100;
//
//            CGRect otherFrame = self.otherBtnContainer.frame;
//            self.otherBtnContainer.sd_y = otherFrame.origin.y + 100;
//
//            CGRect vioFrame = self.vioView.frame;
//            self.vioView.sd_y = vioFrame.origin.y + 100;
//        }];
//    } else {
//        [UIView animateWithDuration:0.2 animations:^{
//
//            CGRect frame = self.zoomBtnContainer.frame;
//            self.zoomBtnContainer.sd_y = frame.origin.y - 100;
//
//            CGRect otherFrame = self.otherBtnContainer.frame;
//            self.otherBtnContainer.sd_y = otherFrame.origin.y - 100;
//
//            CGRect vioFrame = self.vioView.frame;
//            self.vioView.sd_y = vioFrame.origin.y - 100;
//        }];
//    }
}

- (void)returnAction{
    [self.popup dismiss:YES];
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self setWhiteNavigationBar];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [self setDefaultNavigationBar];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
